define(function(require, exports, module) {
    'use strict';
    

    var rk = require('rk');

    var crmGridIntroduce = require('page/js/frame/newFeatureTips/crmGridIntroduce');

    $.widget('rk.pageMainCtrl', {
        _create: function() {
            var me = this;
            var elem = me.element;
        },
        _init: function() {
            var me = this;
            var elem = me.element;

            //me._loadi18n();
            me._loadNotice();
            me._loadNewFeatureTip();

            me.leftCon = elem.find('#pageleft').pageLeftCtrl();
            me.headCon = elem.find('#pagehead').pageHeadCtrl();
            me.contentCon = elem.find('#pagecontent').pageContentCtrl();

            var oaItems = window.SESSION.oaMenuItems;
            var hasPrivateMsg = false;
            for (var i = 0; i < oaItems.length; i++) {
                if (oaItems[i].name === 'privateMessage') {
                    hasPrivateMsg = true;
                    break;
                }
            }

            if (hasPrivateMsg) {
                elem.find('#privatemsg').show();
                var unRead = window.SESSION.privateMsgUnRead;
                if (unRead > 0) {
                    elem.find('#privatemsg').find('.pg-pm-thumb > span.num').show();
                    if (unRead > 99) {
                        unRead = '99+';
                    }
                    elem.find('#privatemsg').find('.pg-pm-thumb > span.num').text(unRead);
                }
                elem.find('#privatemsg').find('.pg-pm-thumb').on('click', function() {
                    if ($('#privaemsg').data('msgInited') === 'privatemsginited') {
                        elem.find('#privatemsg').privateMsgCtrl('showMsg');
                    } else {
                        require.async(['page/js/privatemsg/privateMsgCtrl'], function() {
                            me.msgCon = elem.find('#privatemsg').privateMsgCtrl();
                            elem.find('#privatemsg').privateMsgCtrl('showMsg');
                        });
                    }
                });
            } else {
                elem.find('#privatemsg').remove();
            }

            rk.oa.initScreenWidthHandler(); //最小宽度拖拽滚动条时，oa的面板不应当位移

            me._loadGuider();
        },
        _loadi18n: function() {

        },
        _loadGuider: function() {
            var me = this;
            
            var serverKey = window.SESSION.guideData.userGuide + '';
            if (serverKey != '1') return;

            window.clearTimeout(me.newfeatureTimer);
            require.async('page/js/frame/userGuider/userGuiderCtrl', function() {
                $(window).userGuiderCtrl({done:function(){
                    me.introduceLogoTheme();
                    me.introduceCrmGridFilter();
                    /*crmGridIntroduce.introduceV36ForcastPosition();
                    crmGridIntroduce.introduceFilterSortStep1();*/
                }});
            });
            window.SESSION.guideData.userGuide = 0;
        },
        _loadNotice: function() {
            var bar = $('#maintenance_bar');
            if (bar.size() > 0 && $.cookie('close_maintain_notice') != 'true') {
                bar.show().find('.maintenance-close').click(function() {
                    $.cookie('close_maintain_notice', 'true');
                    bar.hide().off().remove();
                    $('.wrapper-panel').css('bottom', '0px');
                    $('#privatemsg').css('bottom', 0 + 'px');
                    $('#wave_privatemsg').css('bottom', $('#privatemsg').css('bottom'));
                    $(window).trigger('resize');
                });
                $('.wrapper-panel').css('bottom', '40px'); //crm的grid也需要升高
                $('#privatemsg').css('bottom', bar.height() + 'px');
                $(window).trigger('resize');
            }
        },
        _loadNewFeatureTip: function(){
            var me = this;

            me._showChangeLog();
            
            me.newfeatureTimer = window.setTimeout(function(){
                me.introduceLogoTheme();
                me.introduceCrmGridFilter();
                /*新功能导航  销售预测以及列表导航 kill down
                crmGridIntroduce.introduceV36ForcastPosition();
                crmGridIntroduce.introduceFilterSortStep1();
                */
            }, 200);
        },
        _showChangeLog: function(){
            var ctx = '';

            var version = SESSION.currentVersion;
            var cookieVersion = $.cookie('xiaoshouyi_version');
            if (version && version != cookieVersion) {
                $('div.js-latest-update-tip-div').show();
            }

            $('a.js-latest-update-tip').click(function () {
                var $latest = $('div.js-latest-update-content').show();
                $.get(ctx + '/global/nossl-proxy.action?url=http://www.xiaoshouyi.com/helpcenter/index/update/36-update-web.html', null, function (html) {
                    $.removeCookie('xiaoshouyi_version');
                    $.cookie('xiaoshouyi_version', version,   {path: '/', expires: new Date('2099/10/10')});
                    //去掉这个烦人的logo
                    var $h = $(html.replace('http://www.xiaoshouyi.com/helpcenter/images/logo.png', ''));
                    var $content = $h.find('div.blog div:first');
                    // 去掉title的连接
                    $h.find('h2').html('<span>' + $h.find('h2 a').html() + '</span>');
                    // 修正图片引用连接
                    var $img = $content.find('img');
                    $img.attr('src', ctx + '/global/img-proxy.action?url=http://www.xiaoshouyi.com' + $img.attr('src'));
                    // 修正more按钮连接
                    var $more = $content.find('a.btn');
                    $more.attr('href', 'http://www.xiaoshouyi.com' + $more.attr('href')).attr('target', '_blank').addClass('js-readed-latest-version-tip');
                    $latest.find('div.new_remind_pop').html($content.html());
                }, 'text')
            });
        },
        introduceLogoTheme: function() {

        },
        introduceCrmGridFilter: function() {
            return;//不再显示
            var me = this;
            var id = 'newfeature_crmgridfilter';
            if($.cookie(id)) return;

            var filterBtn = $('#div_container_content');
            if(filterBtn.size() == 0)return;//非crm页面不显示
            if(seajs.data.isCRMFinal)return;//crm详情页面不显示

            var timer;
            var counter = 0;
            function waitForGrid(){
                if(counter > 10) window.clearTimeout(timer);
                timer = window.setTimeout(function(){
                    if($('#div_container_content').find('.grid-filter-btn').size()>0){
                        me._introduceCrmGridFilter(id);
                        window.clearTimeout(timer);
                    }else{
                        waitForGrid();
                    }
                    counter++;
                }, 500);
            }
            waitForGrid();
        },
        _introduceCrmGridFilter: function(id){

        }


    });

    return {
        init: function() {
            $('body').pageMainCtrl();
        }
    }
});
